package com.example.lxy.controller;

import com.alibaba.fastjson.JSON;
import com.example.lxy.pojo.dept;
import com.example.lxy.pojo.role;
import com.example.lxy.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@ResponseBody
@Api(value = "角色管理",tags = "角色接口模块")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @CrossOrigin
    @PostMapping(value = "/api/listrole")
    @ApiOperation(value = "查询所有角色",notes = "从角色表中查询所有")
    public String listdept() {
        HashMap<String, Object> data = new HashMap<>();
        List<role> list = roleService.list();
        if (list!=null) {
            data.put("flag",200);
            data.put("roles",list);
        } else {
            data.put("flag",400);
        }
        String s = JSON.toJSONString(data);
        return s;
    }

    @CrossOrigin
    @PostMapping(value = "/api/addrole")
    @ApiOperation(value = "增加角色",notes = "从角色表中增加")
    public String add(@RequestBody role r) {
        System.out.println(r);
        roleService.add(r);
        return "true";
    }

    @CrossOrigin
    @PostMapping(value = "/api/delrole")
    @ApiOperation(value = "删除角色",notes = "从角色表中删除")
    public boolean del(@RequestBody role r) {
        System.out.println(r.getRid());
        boolean del = roleService.del(r.getRid());
        return del;
    }

    @CrossOrigin
    @PostMapping(value = "/api/upidrole")
    @ApiOperation(value = "根据rid查询角色",notes = "根据角色表中的唯一标识RID查询角色")
    public role upid(@RequestBody role r) {
        role upid = roleService.upid(r.getRid());
        return upid;
    }

    @CrossOrigin
    @PostMapping(value = "/api/updaterole")
    @ApiOperation(value = "修改角色",notes = "修改RID查询出来的那条信息")
    public String update(@RequestBody role r) {
        roleService.update(r);
        return "true";
    }
}
